<template>
  <a-modal
    title="选择团队"
    :width="500"
    :visible="visible"
    :footer="null"
    @cancel="handleCancel"
    centered
    :zIndex="1002"
  >
    <div class="team-slider">
      <div class="team-list beauty-scroller-bar" v-if="teamList.length > 0">
        <div
          class="team-item"
          v-for="(item, index) of teamList"
          :key="index"
          @click="onTeamChange(item)"
          :class="currentTeam.id == item.id ? 'team-item-current' : ''"
        >
          <div class="flex align-center" style="flex: 1">
            <img
              :src="item.avatar"
              alt=""
              v-if="item.avatar"
              class="team-avatar"
            />
            <div v-else class="team-avatar-box">
              {{
                item.name && item.name.length > 0 ? item.name.slice(0, 1) : ""
              }}
            </div>
            <div class="mr-10" style="flex: 1 1 0%">
              <div>
				<span>{{ item.name || "-" }}</span>
				<a-tag v-if="item?.id == currentTeam?.id" style="margin-left: 5px;" color="blue">当前帐号</a-tag>
			  </div>
              <div style="color: #666666; font-size: 12px;padding-top:4px;">
                  <a-tag style="border-radius:10px;background: #108ee9 !important;" color="#108ee9" v-if="item.can_edit">主账号</a-tag>
                  <a-tag style="border-radius:10px" color="#2db7f5" v-else>子账号</a-tag>
                <span
                  v-if="item?.team_user_status === 0"
                  style="
                    padding: 2px 5px;
                    background: #f50;
                    color: #fff;
                    border-radius: 5px;
                    cursor: pointer;
                    margin-left: 10px;
                  "
                  >账号已被禁用</span
                >
              </div>
            </div>
          </div>
          <SwapOutlined
            :style="{ color: currentTeam.id == item.id ? '#5a54f9' : '' }"
          />
        </div>
      </div>
    </div>
  </a-modal>
</template>

<script setup>
import { ref, watch, computed, reactive } from "vue";
import { SwapOutlined } from "@ant-design/icons-vue";
import { useTeam, useTeamState } from "../../hooks/team";
import { message } from "ant-design-vue";
import store from "../../store";
import {PermissionConfigs} from '@/common/utils/atherUtils.js'


const emit = defineEmits(["change", "cancel",'ok']);
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
});

const { teams, currentTeam, changeTeamId } = useTeam();

const teamList = computed(() => {
  return store?.state.user?.teamList?.filter((item) => item.id);
});

const onTeamChange = async (team) => {
  if (team?.team_user_status === 0) {
    message.warning("子账号已被禁用，自动切换到主账号");
    await changeTeamId(teamList?.find((item) => item.id && item.can_edit)?.id);
    setTimeout(() => {
      emit("ok");
      PermissionConfigs.clearData()
    }, 0);
    return;
  }

  if (team?.team_user_status === 1) {
    await changeTeamId(team.id);
    setTimeout(() => {
      emit("ok");
      PermissionConfigs.clearData()
    }, 0);
  }
};

const handleCancel = () => {
  emit("cancel");
};
</script>

<style lang="less" scoped>
.team-slider {
  .team-list {
    overflow-y: auto;
  }

  .team-item {
    width: 100%;
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    padding: 10px;
    border-radius: 4px;
    cursor: pointer;

    &:hover {
      background-color: #f5f5f5;
    }

    &:last-child {
      margin-bottom: 0;
    }

    .team-avatar {
      width: 36px;
      height: 36px;
      object-fit: cover;
      border-radius: 50%;
      margin-right: 10px;
    }

    .team-avatar-box {
      width: 36px;
      height: 36px;
      background-color: #1890ff;
      border-radius: 50%;
      margin-right: 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      font-size: 16px;
    }
  }
}
</style>
